package com.ferryc.modules.sys.web;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ferryc.commons.shiro.ShiroDbRealm;
import com.ferryc.commons.utils.StringUtil;
import com.ferryc.commons.web.BaseController;
import com.ferryc.constant.GlobalConstant;
import com.ferryc.constant.ResultConstant;
import com.ferryc.modules.sys.entity.SysRole;
import com.ferryc.modules.sys.entity.param.SysRoleParam;
import com.ferryc.modules.sys.service.ISysRoleService;

/**
 * 
 * 类名: SysRoleController <br/>
 * 作用： 控制<br/>
 * 方法： <br/>
 * 创建者: Ferry C <br/>
 * 添加时间: 2017年12月02日 08:44:23 <br/>
 */
@Controller
@RequestMapping("/role")
public class SysRoleController extends BaseController {

	/** */
	@Autowired
	private ISysRoleService sysRoleService;

	/** */
	@RequiresPermissions("role")
	@GetMapping("/")
	public String manage(Model model) {
		return "sys/role/list";
	}

	/**
	 * 列表查询
	 * 
	 * @param sysRoleParam
	 *            请求参数
	 * @return
	 */
	@RequiresPermissions("role")
	@PostMapping("/list")
	@ResponseBody
	public Object list(SysRoleParam sysRoleParam) {
		sysRoleParam.setStatus(StringUtil.EMPTY);
		return sysRoleService.selectPage(sysRoleParam.toSysRole());
	}

	/**
	 * 进入新增页面
	 * 
	 * @param sysLogInfoParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 *            request
	 * @return
	 */
	@RequiresPermissions("role_add")
	@GetMapping("/addPage")
	public Object addPage(SysRoleParam sysRoleParam, Model model, HttpServletRequest request) {
		model.addAttribute(GlobalConstant.PARENT_ID, request.getParameter(GlobalConstant.PARENT_ID));
		return "sys/role/add";
	}

	/**
	 * 新增保存
	 * 
	 * @param sysRoleParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("role_add")
	@PostMapping("/insert")
	@ResponseBody
	public Object insert(@Valid SysRoleParam sysRoleParam, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			List<FieldError> errorList = bindingResult.getFieldErrors();
			return renderError(errorList);
		}
		boolean flag = sysRoleService.checkRole(sysRoleParam.toSysRole());
		if (!flag) {
			return renderError(ResultConstant.EXISTS_ROLE);
		}
		sysRoleParam.preInsert();
		sysRoleService.insertRole(sysRoleParam.toSysRole());
		return renderSuccess(ResultConstant.INSERT_SUCCESS);
	}

	/**
	 * 进入查看页面
	 * 
	 * @param sysRoleParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 * @return
	 */
	@RequiresPermissions("role_view")
	@GetMapping("/viewPage")
	public Object viewPage(SysRoleParam sysRoleParam, Model model, HttpServletRequest request) {
		sysRoleParam.setStatus(StringUtil.EMPTY);
		SysRole sysRole = sysRoleService.get(sysRoleParam.getId());
		List<SysRole> permIdList = sysRoleService.selectRolePermList(sysRole.getId());
		List<String> permIds = new ArrayList();
		if (!CollectionUtils.isEmpty(permIdList)) {
			for (Iterator iterator = permIdList.iterator(); iterator.hasNext();) {
				SysRole rp = (SysRole) iterator.next();
				permIds.add(rp.getPermissionId());
			}
		}
		model.addAttribute("info", sysRole);
		model.addAttribute("permIds", StringUtil.join(permIds, ","));
		model.addAttribute(GlobalConstant.READ_ONLY, Boolean.TRUE);
		return "sys/role/edit";
	}

	/**
	 * 进入修改页面
	 * 
	 * @param sysRoleParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 * @return
	 */
	@RequiresPermissions("role_update")
	@GetMapping("/editPage")
	public Object editPage(SysRoleParam sysRoleParam, Model model, HttpServletRequest request) {
		sysRoleParam.setStatus(StringUtil.EMPTY);

		SysRole sysRole = sysRoleService.get(sysRoleParam.getId());
		List<SysRole> permIdList = sysRoleService.selectRolePermList(sysRole.getId());
		List<String> permIds = new ArrayList();
		if (!CollectionUtils.isEmpty(permIdList)) {
			for (Iterator iterator = permIdList.iterator(); iterator.hasNext();) {
				SysRole rp = (SysRole) iterator.next();
				permIds.add(rp.getPermissionId());
			}
		}
		model.addAttribute("info", sysRole);
		model.addAttribute("permIds", StringUtil.join(permIds, ","));
		model.addAttribute(GlobalConstant.READ_ONLY, Boolean.FALSE);
		return "sys/role/edit";
	}

	/**
	 * 修改
	 * 
	 * @param sysRoleParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("role_update")
	@PostMapping("/save")
	@ResponseBody
	public Object save(@Valid SysRoleParam sysRoleParam, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			List<FieldError> errorList = bindingResult.getFieldErrors();
			return renderError(errorList);
		}
		boolean flag = sysRoleService.checkRole(sysRoleParam.toSysRole());
		if (!flag) {
			return renderError(ResultConstant.EXISTS_ROLE);
		}
		sysRoleParam.preUpdate();
		sysRoleService.updateRole(sysRoleParam.toSysRole());
		new ShiroDbRealm().clearUserAuth();
		return renderSuccess(ResultConstant.UPDATE_SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param sysRoleParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("role_delete")
	@PostMapping("/delete")
	@ResponseBody
	public Object delete(SysRoleParam sysRoleParam, BindingResult bindingResult) {
		sysRoleParam.preUpdate();
		sysRoleParam.setDelFlag(GlobalConstant.YES);
		sysRoleService.deleteRole(sysRoleParam.toSysRole());
		return renderSuccess(ResultConstant.DELETE_SUCCESS);
	}

	/**
	 * 获取用户角色
	 * 
	 * @param sysUsersParam
	 * @return
	 */
	@PostMapping("/selectRoleList")
	@ResponseBody
	public Object selectRoleList(SysRoleParam sysRoleParam) {
		return sysRoleService.listByEntity(sysRoleParam.toSysRole());
	}

}